package com.meteor.framework.common.filter;

/**
 *
 *
 * @author lynn
 * @date 2024/1/12
 */

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;

/**
 * 请求参数去除前后空格过滤器
 */

@Log4j2
@WebFilter(urlPatterns = {"/*"}, filterName = "ParameterTrimFilter")
public class ParameterTrimFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException {
        log.info("进入请求参数去前后空格过滤器");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //防止流读取一次就没，自定义HttpServletRequestWrapper处理
        ParameterTrimWrapper parameterTrimWrapper = new ParameterTrimWrapper(request);
        filterChain.doFilter(parameterTrimWrapper, servletResponse);
        log.info("参数去空格过滤器处理完成");
    }

    @Override
    public void destroy() {
    }
}
